<?php
include("paging.class.php");
function viewProvince($db){
	
	//----------------------------------------------------------------------------------------
	//  paging   // by default we show first page
	//----------------------------------------------------------------------------------------
	$pagePerStage = 10; //Stage on Paging
	$rowsPerPage = 10;// by default we show first page
	$pageNum = 1;//default page first
	empty($_REQUEST['page'])?$page=1:$page = $_REQUEST['page'];
	empty($_REQUEST['stage'])?$stage=1:$stage = $_REQUEST['stage'];
	$offset = ($page*$rowsPerPage)-($rowsPerPage - 1);
	if($page == 1)$offset = 1;
	$maxRowsPerPage = $page * $rowsPerPage;
	
	$column = "";
	//set column
	$col[0] = array("title"=>"#","name"=>"","hid"=>"desc","width"=>"10%");
	$col[1] = array("title"=>"รหัสจังหวัด","name"=>"","hid"=>"desc","width"=>"15%");
	$col[2] = array("title"=>"จังหวัด","name"=>"","hid"=>"desc","width"=>"15%");
	//$col[3] = array("title"=>"เพิ่ม","name"=>"","hid"=>"desc","width"=>"10%");
	$col[3] = array("title"=>"แก้ไข","name"=>"","hid"=>"desc","width"=>"10%");
	$col[4] = array("title"=>"ลบ","name"=>"","hid"=>"desc","width"=>"10%");	

	$column .="<tr>\n";

	//default order type
	$defaultOrder = $col[1]['name'];
	
	//get Var _GET   order and type
	empty($_REQUEST['Order'])?$order=$defaultOrder:$order = $_REQUEST['Order'];
	if (empty($_REQUEST['Otype']))$type='asc'; else $type = $_REQUEST['Otype'];
	
	//next type
	if ($type=='desc') $ntype = 'asc'; else $ntype = 'desc';
	
	//default img active Or inactive
	$img['asc'] = "<img src='".IMG_DIR."sort_asc.gif' /> ";
	$img['desc'] = "<img src='".IMG_DIR."sort_desc.gif' /> ";
	
	//get param old
	$arr = explode("&",$_SERVER["QUERY_STRING"]);
	if (is_array($arr)) {
	    for ($i=0;$i<count($arr);$i++) {
	        if (!is_int(strpos($arr[$i],"Order=")) && !is_int(strpos($arr[$i],"Otype=")) && trim($arr[$i]) != "") $qs .= $arr[$i]."&";
	    }
	}
	$column .="<tr>\n";
	foreach ($col as $key=>$value) {
	    if($col[$key]['name']!="") {
	    //default Link
	        $col[$key]['link']=$_SERVER['PHP_SELF']."?".$qs."Order=".$col[$key]['name']."&Otype=".$ntype;
	        //image asc or desc
	        if($col[$key]['name']==$order)$col[$key]['img']=$img[$type];
	        else $col[$key]['img']="";
	        $ahefSort = $col[$key]['img']."<a href=\"".$col[$key]['link']."\" >".$col[$key]['title']."</a>";
	    }else $ahefSort =$col[$key]['title'];
	    $column.="	<td align=\"center\" height=\"25px\" width=\"".$col[$key]['width']."\" class=\"formcolumn\" title=\"".$col[$key]['hid']."\">".$ahefSort."</td>\n";
	}
	$column.="</tr>\n";
	//----------------------------------------------------------------------------------------	
	
	$sql = "select * from province order by province_id";
	if($order <>'' && $type <>'') $sql .= " ORDER BY ".$order." ".$type." ";
	$rs = $db->SelectLimit($sql, $rowsPerPage,$offset - 1);
	
	$num = $offset;
	$numrow = $rs->RecordCount();
		
	$num = 1;
	while($arr = $rs->FetchRow()) {
					    
	    $province_id = $arr['province_id'];
	    $province_desc = $arr['province_desc'];		
		$imgEdit = "<input type='image' src='images/edit.gif' border=0 title='แก้ไขจังหวัด' onclick=\"actionEdit('$province_id','$province_desc','edit')\">\n";
		$imgDel = "<input type='image' src='images/del.gif' border=0 title='ลบจังหวัด' onclick=\"cmdProvince('$province_id','del')\">\n";
	    if($num%2) $c="odd"; else $c="even";
	    $result_data .="<tr id='row_".$offset."' class=\"".$c."\" onClick=\"checkClassTR(this,'".$c."');\">\n";
	    $result_data .="	<td align=\"center\" >".$num."</td>\n";
	    $result_data .="	<td align=\"center\" >" . $province_id. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $province_desc. "</td>\n";		    		    
	    //$result_data .="	<td align=\"center\" >" . $imgAdd. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgEdit. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgDel. "</td>\n";
	    $result_data .="</tr>\n";
	    $num++;
	}
	
	$rs->close();

	//----------------------------------------------------------------------------------------
	//  paging Display
	//----------------------------------------------------------------------------------------
	//$sql_s = split("FROM",$sql);
	$sql = "select count(*) ROW_ALL from province ";
	//echo $sql;
	$rs = $db->Execute($sql);
	if ( $arr = $rs->FetchRow()) {
	    $PageClass = new paging($rowsPerPage,$pagePerStage,"map");
	    $PageClass->setPhpself('mprovince.php');
	    $result_paging =  $PageClass->getPaging($arr['ROW_ALL'],$page,$stage);
	}else {
	    print "Mysql error: " . print $conn->ErrorMsg() . "!<br>\n";	die();
	}
	$rs->close();
	$imgAdd = "<input type='image' src='images/add.gif' border=0 title='เพิ่มจังหวัด' onclick=\"actionCmd('add')\"><span class=\"txt_module_head\">&nbsp;&nbsp;เพิ่มจังหวัด</span>\n";			
	return 	'<fieldset><legend class="txt_module_head">รายละเอียด</legend><div align="center">'.$result_paging.'</div>
			<table width="100%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCCC">'						
			.$column.$result_data.'</table><div align="center">'.$result_paging.'</div>'.$imgAdd.'</fieldset>';
	
}


function viewAlarm($db){
	//----------------------------------------------------------------------------------------
	//  paging   // by default we show first page
	//----------------------------------------------------------------------------------------
	$pagePerStage = 10; //Stage on Paging
	$rowsPerPage = 10;// by default we show first page
	$pageNum = 1;//default page first
	empty($_REQUEST['page'])?$page=1:$page = $_REQUEST['page'];
	empty($_REQUEST['stage'])?$stage=1:$stage = $_REQUEST['stage'];
	$offset = ($page*$rowsPerPage)-($rowsPerPage - 1);
	if($page == 1)$offset = 1;
	$maxRowsPerPage = $page * $rowsPerPage;
	
	$column = "";
	//set column
	//$col[0] = array("title"=>"#","name"=>"","hid"=>"desc","width"=>"10%");
	$col[0] = array("title"=>"alarm num","name"=>"","hid"=>"desc","width"=>"10%");
	$col[1] = array("title"=>"alarm desc en","name"=>"","hid"=>"desc","width"=>"15%");
	$col[2] = array("title"=>"alarm desc th","name"=>"","hid"=>"desc","width"=>"15%");
	$col[3] = array("title"=>"Check dup","name"=>"","hid"=>"desc","width"=>"10%");
	$col[4] = array("title"=>"แก้ไข","name"=>"","hid"=>"desc","width"=>"10%");
	$col[5] = array("title"=>"ลบ","name"=>"","hid"=>"desc","width"=>"10%");		
	$col[6] = array("title"=>"ALarm Working","name"=>"","hid"=>"desc","width"=>"10%");		
	$column .="<tr>\n";

	//default order type
	$defaultOrder = $col[1]['name'];
	
	//get Var _GET   order and type
	empty($_REQUEST['Order'])?$order=$defaultOrder:$order = $_REQUEST['Order'];
	if (empty($_REQUEST['Otype']))$type='asc'; else $type = $_REQUEST['Otype'];
	
	//next type
	if ($type=='desc') $ntype = 'asc'; else $ntype = 'desc';
	
	//default img active Or inactive
	$img['asc'] = "<img src='".IMG_DIR."sort_asc.gif' /> ";
	$img['desc'] = "<img src='".IMG_DIR."sort_desc.gif' /> ";
	
	//get param old
	$arr = explode("&",$_SERVER["QUERY_STRING"]);
	if (is_array($arr)) {
	    for ($i=0;$i<count($arr);$i++) {
	        if (!is_int(strpos($arr[$i],"Order=")) && !is_int(strpos($arr[$i],"Otype=")) && trim($arr[$i]) != "") $qs .= $arr[$i]."&";
	    }
	}
	$column .="<tr>\n";
	foreach ($col as $key=>$value) {
	    if($col[$key]['name']!="") {
	    //default Link
	        $col[$key]['link']=$_SERVER['PHP_SELF']."?".$qs."Order=".$col[$key]['name']."&Otype=".$ntype;
	        //image asc or desc
	        if($col[$key]['name']==$order)$col[$key]['img']=$img[$type];
	        else $col[$key]['img']="";
	        $ahefSort = $col[$key]['img']."<a href=\"".$col[$key]['link']."\" >".$col[$key]['title']."</a>";
	    }else $ahefSort =$col[$key]['title'];
	    $column.="	<td align=\"center\" height=\"25px\" width=\"".$col[$key]['width']."\" class=\"formcolumn\" title=\"".$col[$key]['hid']."\">".$ahefSort."</td>\n";
	}
	$column.="</tr>\n";
	//----------------------------------------------------------------------------------------	
	$sql = "select * from alarm_desc order by alarm_num";
	if($order <>'' && $type <>'') $sql .= " ORDER BY ".$order." ".$type." ";
	$rs = $db->SelectLimit($sql, $rowsPerPage,$offset - 1);
	
	$num = $offset;
	$numrow = $rs->RecordCount();
		
	$num = 1;
	while($arr = $rs->FetchRow()) {
					    
	    $alarm_num = $arr['alarm_num'];
	    $alarm_desc_en = $arr['alarm_desc_en'];		
	    $alarm_desc_th = $arr['alarm_desc_th'];		
	    $chk_dup = $arr['check_dup'];
	    
		$imgEdit = "<input type='image' src='images/edit.gif' border=0 title='แก้ไข alarm' onclick=\"actionEdit('$alarm_num','$alarm_desc_en','$alarm_desc_th','$chk_dup','edit')\">\n";
		$imgDel = "<input type='image' src='images/del.gif' border=0 title='ลบ alarm' onclick=\"cmdAlarm('$alarm_num','del')\">\n";
		$imgDay = "<input type='image' src='images/day.gif' border=0 title='กำหนดวันเวลาทำงานของ ALarm' onclick=\"cmdDayAlarm('$alarm_num')\">\n";
	    if($num%2) $c="odd"; else $c="even";
	    $result_data .="<tr id='row_".$offset."' class=\"".$c."\" onClick=\"checkClassTR(this,'".$c."');\">\n";
	    //$result_data .="	<td align=\"center\" >".$num."</td>\n";
	    $result_data .="	<td align=\"center\" >" . $alarm_num. "</td>\n";
	    $result_data .="	<td align=\"left\" >" . $alarm_desc_en. "</td>\n";		    		    
	    $result_data .="	<td align=\"left\" >" . $alarm_desc_th. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $chk_dup. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgEdit. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgDel. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgDay. "</td>\n";
	    $result_data .="</tr>\n";
	    $num++;
	}
	
	$rs->close();
	
	//----------------------------------------------------------------------------------------
	//  paging Display
	//----------------------------------------------------------------------------------------
	//$sql_s = split("FROM",$sql);
	$sql = "select count(*) ROW_ALL from alarm_desc ";
	//echo $sql;
	$rs = $db->Execute($sql);
	if ( $arr = $rs->FetchRow()) {
	    $PageClass = new paging($rowsPerPage,$pagePerStage,"map");
	    $PageClass->setPhpself('malarm.php');
	    $result_paging =  $PageClass->getPaging($arr['ROW_ALL'],$page,$stage);
	}else {
	    print "Mysql error: " . print $conn->ErrorMsg() . "!<br>\n";	die();
	}
	$rs->close();
	$imgAdd = "<input type='image' src='images/add.gif' border=0 title='เพิ่ม ALarm' onclick=\"actionCmd('add')\"><span class=\"txt_module_head\">&nbsp;&nbsp;เพิ่ม ALarm</span>\n";			
	return 	'<fieldset><legend class="txt_module_head">รายละเอียด</legend><div align="center">'.$result_paging.'</div>
			<table width="100%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCCC">'						
			.$column.$result_data.'</table><div align="center">'.$result_paging.'</div>'.$imgAdd.'</fieldset>';
	
}


function viewExchName($db){
	//----------------------------------------------------------------------------------------
	//  paging   // by default we show first page
	//----------------------------------------------------------------------------------------
	$pagePerStage = 10; //Stage on Paging
	$rowsPerPage = 10;// by default we show first page
	$pageNum = 1;//default page first
	empty($_REQUEST['page'])?$page=1:$page = $_REQUEST['page'];
	empty($_REQUEST['stage'])?$stage=1:$stage = $_REQUEST['stage'];
	$offset = ($page*$rowsPerPage)-($rowsPerPage - 1);
	if($page == 1)$offset = 1;
	$maxRowsPerPage = $page * $rowsPerPage;
	
	$column = "";
	//set column
	//$col[0] = array("title"=>"#","name"=>"","hid"=>"desc","width"=>"10%");
	$col[0] = array("title"=>"Location","name"=>"","hid"=>"desc","width"=>"10%");
	$col[1] = array("title"=>"ชื่อชุมสาย","name"=>"","hid"=>"desc","width"=>"15%");
	$col[2] = array("title"=>"จังหวัด","name"=>"","hid"=>"desc","width"=>"15%");
	$col[3] = array("title"=>"แก้ไข","name"=>"","hid"=>"desc","width"=>"10%");
	$col[4] = array("title"=>"ลบ","name"=>"","hid"=>"desc","width"=>"10%");		
	
	$column .="<tr>\n";

	//default order type
	$defaultOrder = $col[1]['name'];
	
	//get Var _GET   order and type
	empty($_REQUEST['Order'])?$order=$defaultOrder:$order = $_REQUEST['Order'];
	if (empty($_REQUEST['Otype']))$type='asc'; else $type = $_REQUEST['Otype'];
	
	//next type
	if ($type=='desc') $ntype = 'asc'; else $ntype = 'desc';
	
	//default img active Or inactive
	$img['asc'] = "<img src='".IMG_DIR."sort_asc.gif' /> ";
	$img['desc'] = "<img src='".IMG_DIR."sort_desc.gif' /> ";
	
	//get param old
	$arr = explode("&",$_SERVER["QUERY_STRING"]);
	if (is_array($arr)) {
	    for ($i=0;$i<count($arr);$i++) {
	        if (!is_int(strpos($arr[$i],"Order=")) && !is_int(strpos($arr[$i],"Otype=")) && trim($arr[$i]) != "") $qs .= $arr[$i]."&";
	    }
	}
	$column .="<tr>\n";
	foreach ($col as $key=>$value) {
	    if($col[$key]['name']!="") {
	    //default Link
	        $col[$key]['link']=$_SERVER['PHP_SELF']."?".$qs."Order=".$col[$key]['name']."&Otype=".$ntype;
	        //image asc or desc
	        if($col[$key]['name']==$order)$col[$key]['img']=$img[$type];
	        else $col[$key]['img']="";
	        $ahefSort = $col[$key]['img']."<a href=\"".$col[$key]['link']."\" >".$col[$key]['title']."</a>";
	    }else $ahefSort =$col[$key]['title'];
	    $column.="	<td align=\"center\" height=\"25px\" width=\"".$col[$key]['width']."\" class=\"formcolumn\" title=\"".$col[$key]['hid']."\">".$ahefSort."</td>\n";
	}
	$column.="</tr>\n";
	//----------------------------------------------------------------------------------------	
	$sql = "SELECT e.location_id, e.exch_name, p.province_desc, p.province_id FROM exchange AS e ";
	$sql .="Inner Join province AS p ON p.province_id = e.province_id ORDER BY e.province_id ASC, e.location_id ASC ";
	if($order <>'' && $type <>'') $sql .= " ORDER BY ".$order." ".$type." ";
	$rs = $db->SelectLimit($sql, $rowsPerPage,$offset - 1);
	
	$num = $offset;
	$numrow = $rs->RecordCount();
		
	$num = 1;
	while($arr = $rs->FetchRow()) {
					    
	    $location_id = $arr['location_id'];
	    $exch_name = $arr['exch_name'];		
	    $province_desc = $arr['province_desc'];		
	    $province_id = $arr['province_id'];
	    
		$imgEdit = "<input type='image' src='images/edit.gif' border=0 title='แก้ไข ชุมสาย' onclick=\"actionEdit('$location_id','$exch_name','$province_id')\">\n";
		$imgDel = "<input type='image' src='images/del.gif' border=0 title='ลบชุมสาย' onclick=\"cmdExchName('$location_id','del')\">\n";
	    if($num%2) $c="odd"; else $c="even";
	    $result_data .="<tr id='row_".$offset."' class=\"".$c."\" onClick=\"checkClassTR(this,'".$c."');\">\n";
	    //$result_data .="	<td align=\"center\" >".$num."</td>\n";
	    $result_data .="	<td align=\"center\" >" . $location_id. "</td>\n";
	    $result_data .="	<td align=\"left\" >" . $exch_name. "</td>\n";		    		    
	    $result_data .="	<td align=\"left\" >" . $province_desc. "</td>\n";	    
	    $result_data .="	<td align=\"center\" >" . $imgEdit. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgDel. "</td>\n";
	    $result_data .="</tr>\n";
	    $num++;
	}
	
	$rs->close();
	
	//----------------------------------------------------------------------------------------
	//  paging Display
	//----------------------------------------------------------------------------------------
	//$sql_s = split("FROM",$sql);
	$sql = "select count(*) ROW_ALL FROM exchange AS e ";
	$sql .="Inner Join province AS p ON p.province_id = e.province_id ";
	//echo $sql;
	$rs = $db->Execute($sql);
	if ( $arr = $rs->FetchRow()) {
	    $PageClass = new paging($rowsPerPage,$pagePerStage,"map");
	    $PageClass->setPhpself('mexchname.php');
	    $result_paging =  $PageClass->getPaging($arr['ROW_ALL'],$page,$stage);
	}else {
	    print "Mysql error: " . print $conn->ErrorMsg() . "!<br>\n";	die();
	}
	$rs->close();
	$imgAdd = "<input type='image' src='images/add.gif' border=0 title='เพิ่มชื่อชุมสาย' onclick=\"actionCmd('add')\"><span class=\"txt_module_head\">&nbsp;&nbsp;เพิ่มชื่อชุมสาย</span>\n";			
	return 	'<fieldset><legend class="txt_module_head">รายละเอียด</legend><div align="center">'.$result_paging.'</div>
			<table width="100%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCCC">'						
			.$column.$result_data.'</table><div align="center">'.$result_paging.'</div>'.$imgAdd.'</fieldset>';
	
}

function viewEmpName($db){
	//----------------------------------------------------------------------------------------
	//  paging   // by default we show first page
	//----------------------------------------------------------------------------------------
	$pagePerStage = 10; //Stage on Paging
	$rowsPerPage = 10;// by default we show first page
	$pageNum = 1;//default page first
	empty($_REQUEST['page'])?$page=1:$page = $_REQUEST['page'];
	empty($_REQUEST['stage'])?$stage=1:$stage = $_REQUEST['stage'];
	$offset = ($page*$rowsPerPage)-($rowsPerPage - 1);
	if($page == 1)$offset = 1;
	$maxRowsPerPage = $page * $rowsPerPage;
	
	$column = "";
	//set column
	//$col[0] = array("title"=>"#","name"=>"","hid"=>"desc","width"=>"10%");
	$col[0] = array("title"=>"ชื่อ - นามสกุล","name"=>"","hid"=>"desc","width"=>"15%");
	$col[1] = array("title"=>"ตำแหน่ง","name"=>"","hid"=>"desc","width"=>"10%");
	$col[2] = array("title"=>"ประจำจังหวัด","name"=>"","hid"=>"desc","width"=>"10%");
	$col[3] = array("title"=>"มือถือ","name"=>"","hid"=>"desc","width"=>"10%");
	$col[4] = array("title"=>"แก้ไข","name"=>"","hid"=>"desc","width"=>"8%");
	$col[5] = array("title"=>"ลบ","name"=>"","hid"=>"desc","width"=>"8%");		
	$col[6] = array("title"=>"WorkDay","name"=>"","hid"=>"desc","width"=>"8%");		
	$col[7] = array("title"=>"ดูแลชุมสาย","name"=>"","hid"=>"desc","width"=>"10%");		
	$col[8] = array("title"=>"รับ ALarm","name"=>"","hid"=>"desc","width"=>"8%");		
	
	$column .="<tr>\n";

	//default order type
	$defaultOrder = $col[0]['name'];
	
	//get Var _GET   order and type
	empty($_REQUEST['Order'])?$order=$defaultOrder:$order = $_REQUEST['Order'];
	if (empty($_REQUEST['Otype']))$type='asc'; else $type = $_REQUEST['Otype'];
	
	//next type
	if ($type=='desc') $ntype = 'asc'; else $ntype = 'desc';
	
	//default img active Or inactive
	$img['asc'] = "<img src='".IMG_DIR."sort_asc.gif' /> ";
	$img['desc'] = "<img src='".IMG_DIR."sort_desc.gif' /> ";
	
	//get param old
	$arr = explode("&",$_SERVER["QUERY_STRING"]);
	if (is_array($arr)) {
	    for ($i=0;$i<count($arr);$i++) {
	        if (!is_int(strpos($arr[$i],"Order=")) && !is_int(strpos($arr[$i],"Otype=")) && trim($arr[$i]) != "") $qs .= $arr[$i]."&";
	    }
	}
	$column .="<tr>\n";
	foreach ($col as $key=>$value) {
	    if($col[$key]['name']!="") {
	    //default Link
	        $col[$key]['link']=$_SERVER['PHP_SELF']."?".$qs."Order=".$col[$key]['name']."&Otype=".$ntype;
	        //image asc or desc
	        if($col[$key]['name']==$order)$col[$key]['img']=$img[$type];
	        else $col[$key]['img']="";
	        $ahefSort = $col[$key]['img']."<a href=\"".$col[$key]['link']."\" >".$col[$key]['title']."</a>";
	    }else $ahefSort =$col[$key]['title'];
	    $column.="	<td align=\"center\" height=\"25px\" width=\"".$col[$key]['width']."\" class=\"formcolumn\" title=\"".$col[$key]['hid']."\">".$ahefSort."</td>\n";
	}
	$column.="</tr>\n";
	//----------------------------------------------------------------------------------------	
	$sql = "SELECT e.emp_id, e.province_id, e.emp_name, e.pos_id, e.mobile, p.province_desc, po.position_desc, e.flag_req_spacial FROM employee AS e ";
	$sql .="Inner Join province AS p ON e.province_id = p.province_id Inner Join `position` AS po ON e.pos_id = po.pos_id ";
	$sql .="order by e.emp_name ";
	//echo "====>[$order][$type]";
	
	if($order <>'' && $type <>''){ 
		$sql .= " ORDER BY ".$order." ".$type." ";
	}
	$rs = $db->SelectLimit($sql, $rowsPerPage,$offset - 1);
	
	$num = $offset;
	$numrow = $rs->RecordCount();
		
	$num = 1;
	while($arr = $rs->FetchRow()) {
		$emp_id = $arr['emp_id'];					    
	    $emp_name = $arr['emp_name'];
	    $position_desc = $arr['position_desc'];		
	    $pos_id = $arr['pos_id'];
	    $province_desc = $arr['province_desc'];		
	    $province_id = $arr['province_id'];
	    $mobile = $arr['mobile'];
	    $spacial = $arr['flag_req_spacial'];
	    
		$imgEdit = "<input type='image' src='images/edit.gif' border=0 title='แก้ไข พนักงาน' onclick=\"actionEdit('$emp_id','$emp_name','$pos_id','$province_id','$mobile','$spacial')\">\n";
		$imgDel = "<input type='image' src='images/del.gif' border=0 title='ลบ พนักงาน' onclick=\"cmdEmpName('$emp_id','del')\">\n";
		$imgDay = "<input type='image' src='images/day.gif' border=0 title='กำหนดวันเวลาทำงาน' onclick=\"cmdDayWork('$emp_id')\">\n";		
		$imgExch = "<input type='image' src='images/ex.gif' border=0 title='กำหนดดูแลชุมสาย' onclick=\"showExch('$emp_id','$emp_name')\">\n";
		$imgAlarm = "<input type='image' src='images/spacial.gif' border=0 title='รับ ALarm' onclick=\"showALarm('$emp_id','$emp_name')\">\n";
		/*
		if($spacial == 'Y'){
			//$imgSpacial = "<input type='image' src='images/spacial.gif' border=0 title='กำหนดดูแลชุมสาย' onclick=\"cmdExch('$emp_id','$emp_name')\">\n";
			$imgExch = "<input type='image' src='images/spacial.gif' border=0 title='กำหนดดูแลชุมสาย' onclick=\"showExch('$emp_id','$emp_name')\">\n";
		}else{
			$imgExch = "<input type='image' src='images/ex.gif' border=0 title='กำหนดดูแลชุมสาย' onclick=\"showExch('$emp_id','$emp_name')\">\n";
			//$imgSpacial = "&nbsp;";
		}
		*/
		
	    if($num%2) $c="odd"; else $c="even";
	    $result_data .="<tr id='row_".$offset."' class=\"".$c."\" onClick=\"checkClassTR(this,'".$c."');\">\n";
	    //$result_data .="	<td align=\"center\" >".$num."</td>\n";
	    $result_data .="	<td align=\"left\" >" . $emp_name. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $position_desc. "</td>\n";		    		    
	    $result_data .="	<td align=\"center\" >" . $province_desc. "</td>\n";	    
	    $result_data .="	<td align=\"center\" >" . $mobile. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgEdit. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgDel. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgDay. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgExch. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgAlarm. "</td>\n";
	    $result_data .="</tr>\n";
	    $num++;
	}
	
	$rs->close();
	
	//----------------------------------------------------------------------------------------
	//  paging Display
	//----------------------------------------------------------------------------------------
	//$sql_s = split("FROM",$sql);
	$sql = "SELECT count(*) ROW_ALL FROM employee AS e ";
	$sql .="Inner Join province AS p ON e.province_id = p.province_id Inner Join `position` AS po ON e.pos_id = po.pos_id ";
	//echo $sql;
	$rs = $db->Execute($sql);
	if ( $arr = $rs->FetchRow()) {
	    $PageClass = new paging($rowsPerPage,$pagePerStage,"map");
	    $PageClass->setPhpself('memp.php');
	    $result_paging =  $PageClass->getPaging($arr['ROW_ALL'],$page,$stage);
	}else {
	    print "Mysql error: " . print $conn->ErrorMsg() . "!<br>\n";	die();
	}
	$rs->close();
	
	$imgAdd = "<input type='image' src='images/add.gif' border=0 title='เพิ่มชื่อพนักงาน' onclick=\"actionCmd('add')\"><span class=\"txt_module_head\">&nbsp;&nbsp;เพิ่มชื่อพนักงาน</span>\n";			
	return 	'<fieldset><legend class="txt_module_head">รายละเอียด</legend><div align="center">'.$result_paging.'</div>
			<table width="100%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCCC">'						
			.$column.$result_data.'</table><div align="center">'.$result_paging.'</div>'.$imgAdd.'</fieldset>';
	
}

function getMainternance($db, $mid, $sid, $prov){
		
	$html .='<table width="100%" border="0" cellspacing="3" cellpadding="2">';
	$html .='<tr><td width="30%" align="right" class="line_dash" ><span class="txt_module">ผู้จัดการ </span> : </td>';
	$html .='<td class="line_dash"><select name="manager" id="manager" class="combo">' . getEmployee($db, "1", $mid) . '</select><span class="required"> * ขึ้นตรงกับผู้จัดการ</span></td>';
	$html .='</tr><tr><td width="30%" align="right" class="line_dash" ><span class="txt_module">หัวหน้าหน่วย</span> : </td>';
	$html .='<td class="line_dash"><select name="supervisor" id="supervisor" class="combo">' .getEmployee($db, "2", $sid) . '</select><span class="required"> * ขึ้นตรงกับหัวหน้าหน่วย</span></td>';
	$html .='</tr><tr><td width="30%" align="right" class="line_dash" ><span class="txt_module">จังหวัด</span> : </td>';
	$html .='<td class="line_dash"><select name="ls_provexch" id="ls_provexch" class="combo" onchange="getExch();">' . getProvince($db, $prov) . '</select><span class="required"> * รับผิดชอบจังหวัดใด</span></td>';
	$html .='</tr></table>';			  
	return $html;	
}

function viewMsu($db){
	//----------------------------------------------------------------------------------------
	//  paging   // by default we show first page
	//----------------------------------------------------------------------------------------
	$pagePerStage = 10; //Stage on Paging
	$rowsPerPage = 10;// by default we show first page
	$pageNum = 1;//default page first
	empty($_REQUEST['page'])?$page=1:$page = $_REQUEST['page'];
	empty($_REQUEST['stage'])?$stage=1:$stage = $_REQUEST['stage'];
	$offset = ($page*$rowsPerPage)-($rowsPerPage - 1);
	if($page == 1)$offset = 1;
	$maxRowsPerPage = $page * $rowsPerPage;
	
	$column = "";
	//set column
	//$col[0] = array("title"=>"#","name"=>"","hid"=>"desc","width"=>"10%");
	$col[0] = array("title"=>"ชื่อ MSU","name"=>"","hid"=>"desc","width"=>"15%");
	$col[1] = array("title"=>"ที่ติดตั้ง MSU","name"=>"","hid"=>"desc","width"=>"15%");
	$col[2] = array("title"=>"แก้ไข","name"=>"","hid"=>"desc","width"=>"10%");
	$col[3] = array("title"=>"ลบ","name"=>"","hid"=>"desc","width"=>"10%");		
	
	$column .="<tr>\n";

	//default order type
	$defaultOrder = $col[1]['name'];
	
	//get Var _GET   order and type
	empty($_REQUEST['Order'])?$order=$defaultOrder:$order = $_REQUEST['Order'];
	if (empty($_REQUEST['Otype']))$type='asc'; else $type = $_REQUEST['Otype'];
	
	//next type
	if ($type=='desc') $ntype = 'asc'; else $ntype = 'desc';
	
	//default img active Or inactive
	$img['asc'] = "<img src='".IMG_DIR."sort_asc.gif' /> ";
	$img['desc'] = "<img src='".IMG_DIR."sort_desc.gif' /> ";
	
	//get param old
	$arr = explode("&",$_SERVER["QUERY_STRING"]);
	if (is_array($arr)) {
	    for ($i=0;$i<count($arr);$i++) {
	        if (!is_int(strpos($arr[$i],"Order=")) && !is_int(strpos($arr[$i],"Otype=")) && trim($arr[$i]) != "") $qs .= $arr[$i]."&";
	    }
	}
	$column .="<tr>\n";
	foreach ($col as $key=>$value) {
	    if($col[$key]['name']!="") {
	    //default Link
	        $col[$key]['link']=$_SERVER['PHP_SELF']."?".$qs."Order=".$col[$key]['name']."&Otype=".$ntype;
	        //image asc or desc
	        if($col[$key]['name']==$order)$col[$key]['img']=$img[$type];
	        else $col[$key]['img']="";
	        $ahefSort = $col[$key]['img']."<a href=\"".$col[$key]['link']."\" >".$col[$key]['title']."</a>";
	    }else $ahefSort =$col[$key]['title'];
	    $column.="	<td align=\"center\" height=\"25px\" width=\"".$col[$key]['width']."\" class=\"formcolumn\" title=\"".$col[$key]['hid']."\">".$ahefSort."</td>\n";
	}
	$column.="</tr>\n";
	//----------------------------------------------------------------------------------------	
	$sql = "SELECT n.ncen_code, n.ncen_name, n.province_id, p.province_desc FROM ncen AS n ";
	$sql .="Inner Join province AS p ON n.province_id = p.province_id ";
	$sql .="order by n.province_id ";
	
	if($order <>'' && $type <>'') $sql .= " ORDER BY ".$order." ".$type." ";
	$rs = $db->SelectLimit($sql, $rowsPerPage,$offset - 1);
	
	$num = $offset;
	$numrow = $rs->RecordCount();
		
	$num = 1;
	while($arr = $rs->FetchRow()) {
		$ncen_code = $arr['ncen_code'];					    
	    $ncen_name = $arr['ncen_name'];
	    $province_id = $arr['province_id'];		
	    $province_desc = $arr['province_desc'];		
	    
		$imgEdit = "<input type='image' src='images/edit.gif' border=0 title='แก้ไข MSU' onclick=\"actionEdit('$ncen_name','$province_id')\">\n";
		$imgDel = "<input type='image' src='images/del.gif' border=0 title='ลบ MSU' onclick=\"cmdMsu('$ncen_name','del')\">\n";
	    if($num%2) $c="odd"; else $c="even";
	    $result_data .="<tr id='row_".$offset."' class=\"".$c."\" onClick=\"checkClassTR(this,'".$c."');\">\n";
	    //$result_data .="	<td align=\"center\" >".$num."</td>\n";
	    $result_data .="	<td align=\"center\" >" . $ncen_name. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $province_desc. "</td>\n";		    		    	    	    	    
	    $result_data .="	<td align=\"center\" >" . $imgEdit. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $imgDel. "</td>\n";
	    $result_data .="</tr>\n";
	    $num++;
	}
	
	$rs->close();
	
	//----------------------------------------------------------------------------------------
	//  paging Display
	//----------------------------------------------------------------------------------------
	//$sql_s = split("FROM",$sql);
	$sql = "SELECT count(*) ROW_ALL FROM ncen AS n ";
	$sql .="Inner Join province AS p ON n.province_id = p.province_id ";	//echo $sql;
	$rs = $db->Execute($sql);
	if ( $arr = $rs->FetchRow()) {
	    $PageClass = new paging($rowsPerPage,$pagePerStage,"map");
	    $PageClass->setPhpself('mmsu.php');
	    $result_paging =  $PageClass->getPaging($arr['ROW_ALL'],$page,$stage);
	}else {
	    print "Mysql error: " . print $conn->ErrorMsg() . "!<br>\n";	die();
	}
	$rs->close();
	$imgAdd = "<input type='image' src='images/add.gif' border=0 title='เพิ่มชื่อ MSU' onclick=\"actionCmd('add')\"><span class=\"txt_module_head\">&nbsp;&nbsp;เพิ่มชื่อ MSU</span>\n";			
	return 	'<fieldset><legend class="txt_module_head">รายละเอียด</legend><div align="center">'.$result_paging.'</div>
			<table width="100%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCCC">'						
			.$column.$result_data.'</table><div align="center">'.$result_paging.'</div>'.$imgAdd.'</fieldset>';
	
}


function viewAlarmEmp($db, $empid){
	$column = "";
	//set column	
	$col[0] = array("title"=>"alarm number","name"=>"","hid"=>"desc","width"=>"15%");
	$col[1] = array("title"=>"alarm discription","name"=>"","hid"=>"desc","width"=>"15%");
	$col[2] = array("title"=>"วันที่รับ Alarm","name"=>"","hid"=>"desc","width"=>"15%");
	$col[3] = array("title"=>"เวลาเิ่ริ่มต้นรับ Alarm","name"=>"","hid"=>"desc","width"=>"15%");		
	$col[4] = array("title"=>"เวลาสิ้นสุดรับ Alarm","name"=>"","hid"=>"desc","width"=>"15%");		
	
	$column .="<tr>\n";

    foreach ($col as $key=>$value) {
     $ahefSort =$col[$key]['title'];
     $column.="	<td align=\"center\" height=\"25px\" width=\"".$col[$key]['width']."\" class=\"formcolumn\" title=\"".$col[$key]['hid']."\">".$ahefSort."</td>\n";
    }
    $column.="</tr>\n";

    $sql = "SELECT eo.emp_name, eo.mobile,mainternace.location_id, em.emp_name as em, es.emp_name as es, pv.province_desc,";
	$sql .="po.position_desc, eo.emp_id as eo, eo.flag_req_spacial FROM employee AS eo Inner Join mainternace ON eo.emp_id = mainternace.e_id ";
	$sql .="Inner Join employee AS em ON mainternace.m_id = em.emp_id Inner Join employee AS es ON mainternace.s_id = es.emp_id ";
	$sql .="Inner Join province AS pv ON pv.province_id = eo.province_id Inner Join `position` AS po ON po.pos_id = eo.pos_id ";
	$sql .="WHERE $sql_pos =  '$empid' ";
	$rs = $db->Execute($sql);
	$num = 1;
	while($arr = $rs->FetchRow()) {
		$emp_name = $arr['emp_name'];									    
	    $em = $arr['em'];
	    $es = $arr['es'];
	    $eo = $arr['eo'];
	    $location_id = $arr['location_id'];		
	    $province_desc = $arr['province_desc'];	    		
	    
	    if($num%2) $c="odd"; else $c="even";
	    $result_data .="<tr id='row_".$offset."' class=\"".$c."\" onClick=\"checkClassTR(this,'".$c."');\">\n";
	    $result_data .="	<td align=\"center\" >" . $emp_name. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $em. "</td>\n";		    		    	    	    	    
	    $result_data .="	<td align=\"center\" >" . $es. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $province_desc. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $location_id. "</td>\n";	    
	    $result_data .="</tr>\n";	  
	    $num++;  
	}
	
	$rs->close();
	
	$sql = "select emp_name, flag_req_spacial from employee where emp_id=?";
	$stmt = $db->Prepare($sql);
	$rs = $db->Execute($stmt, array($empid));
	if($arr = $rs->FetchRow()){
		$empname = $arr['emp_name'];		
		$flag = $arr['flag_req_spacial'];		
	}
	$rs->close();
	$head = '<fieldset><legend class="txt_module_head">'.$empname.' รับผิดชอบชุมสาย</legend>';	
	$imgEdit = "<br /><input type='image' src='images/add.gif' border=0 title='แก้ไขผู้รับผิดชอบชุมสาย' onclick=\"cmdExch('$eo','$emp_name','$flag')\"><span class=\"txt_module_head\">&nbsp;&nbsp;แก้ไขผู้รับผิดชอบชุมสาย</span>\n";
    $result_data = '<table width="100%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCCC">'.$column.''.$result_data.'</table>'.$imgEdit;
    return $head.$result_data;
	
}

function viewMainternce($db, $empid){
	$sql = "select pos_id, province_id from employee where emp_id=?";
	$stmt = $db->Prepare($sql);
	$rs = $db->Execute($stmt, array($empid));
	if($arr = $rs->FetchRow()){
		$pos_id = $arr['pos_id'];
		$prov = $arr['province_id'];
	}
	$rs->close();
	
	if($pos_id != 3){
		return "funcion used for operator!";
	}	
	
    $sql = "SELECT eo.emp_name, eo.mobile, mt.location_id, em.emp_name AS em, es.emp_name AS es,  ";
	$sql .="po.position_desc, eo.flag_req_spacial, mt.m_id, mt.s_id, mt.e_id FROM employee AS eo ";
	$sql .="Inner Join mainternace AS mt ON eo.emp_id = mt.e_id Inner Join employee AS em ON mt.m_id = em.emp_id ";
	$sql .="Inner Join employee AS es ON mt.s_id = es.emp_id	";
	$sql .="Inner Join `position` AS po ON po.pos_id = eo.pos_id WHERE mt.e_id =  ?";
	$stmt = $db->Prepare($sql);
	$rs = $db->Execute($stmt, array($empid));
	$num = 1;
	$html = "";
	$arr_location = array();	
	$index = 0;
	while($arr1 = $rs->FetchRow()) {	    
	    $arr_location[$index] = $arr1['location_id'];
	    $index++;		     
	    if($num == 1){
	    	//echo "[".$m_id."]";
		    $m_id = $arr1['m_id'];
		    $s_id = $arr1['s_id'];	   	    
		    //$prov = $arr1['province_id']; 	    	
	    	//$getmt = getMainternance($db, $m_id, $s_id, $prov);
	    }	    	    
	    $num++;	    		
	    
	}
	$rs->close();
	$getmt = getMainternance($db, $m_id, $s_id, $prov);	
	$html = viewExchEmp($db, $arr_location, $prov);
	//echo $html;
	return $getmt.$html;
}

function viewExchEmp($db, $arrLocation, $prov){
	//print_r($arrLocation);
	$sql = "SELECT e.location_id, e.exch_name, e.exch_addr, e.province_id FROM exchange AS e WHERE e.province_id = ? order by location_id";
	$stmt = $db->Prepare($sql);
	$arr_pre = array($prov);
	$rs = $db->Execute($stmt, $arr_pre);
	$html = '';
	//$chk = "";
	while($arr1 = $rs->FetchRow()){
		$html .='<table width="100%" border="0" cellspacing="3" cellpadding="2"><tr>';
		if($arr1){
			//print_r(array_search($arr1['location_id'], $arrLocation));
			if(array_search($arr1['location_id'], $arrLocation) > -1){
				$chk = "checked";
			}else{
				$chk = "";
			}
			$html .='<td width="25%" align="rigth" class="line_dash" ><input type="checkbox" name="location" '.$chk.' value="'.$arr1['location_id'].'"/><span class="txt_module">'.$arr1['location_id'].'</span></td>';
		}else{
			$html .='<td width="25%" align="rigth" class="line_dash" >&nbsp;</td>';
		}
		if($arr2 = $rs->FetchRow()){
			if(array_search($arr2['location_id'], $arrLocation) > -1){
				$chk = "checked";
			}else{
				$chk = "";
			}
			$html .='<td width="25%" align="rigth" class="line_dash" ><input type="checkbox" name="location" '.$chk.' value="'.$arr2['location_id'].'"/><span class="txt_module">'.$arr2['location_id'].'</span></td>';
		}else{
			$html .='<td width="25%" align="rigth" class="line_dash" >&nbsp;</td>';
		}
		if($arr3 = $rs->FetchRow()){
			if(array_search($arr3['location_id'], $arrLocation) > -1){
				$chk = "checked";
			}else{
				$chk = "";
			}
			$html .='<td width="25%" align="rigth" class="line_dash" ><input type="checkbox" name="location" '.$chk.' value="'.$arr3['location_id'].'"/><span class="txt_module">'.$arr3['location_id'].'</span></td>';
		}else{
			$html .='<td width="25%" align="rigth" class="line_dash" >&nbsp;</td>';
		}
		if($arr4 = $rs->FetchRow()){
			if(array_search($arr4['location_id'], $arrLocation) > -1){
				$chk = "checked";
			}else{
				$chk = "";
			}
			$html .='<td width="25%" align="rigth" class="line_dash" ><input type="checkbox" name="location" '.$chk.' value="'.$arr4['location_id'].'"/><span class="txt_module">'.$arr4['location_id'].'</span></td>';		
		}else{
			$html .='<td width="25%" align="rigth" class="line_dash" >&nbsp;</td>';
		}
		$html .='</tr><div id="show"></div></table>';	
	}
	$rs->close();
	//$imgEdit = "<br /><input type='image' src='images/add.gif' border=0 title='แก้ไขผู้รับผิดชอบชุมสาย' onclick=\"cmdExch('$empid','$emp_name','$flag')\"><span class=\"txt_module_head\">&nbsp;&nbsp;แก้ไขผู้รับผิดชอบชุมสาย</span><div id=\"editexch\"></div>\n";    
	$imgEdit = '<br /><img src="images/ok-32.png" alt="" width="32" height="19" onclick="editExch();" />';
	return $html.$imgEdit;
}

function viewMt($db, $empid){

	$sql = "select pos_id from employee where emp_id=?";
	$stmt = $db->Prepare($sql);
	$rs = $db->Execute($stmt, array($empid));
	if($arr = $rs->FetchRow()){
		$pos_id = $arr['pos_id'];
	}
	$rs->close();
	$sql_pos = "";
	switch($pos_id){
		case 1:
			$sql_pos = "mainternace.m_id";
			break;
		case 2:
			$sql_pos = "mainternace.s_id";
			break;
		case 3:
			$sql_pos = "mainternace.e_id";
			break;
			
	}
	$column = "";
	//set column	
	$col[0] = array("title"=>"#","name"=>"","hid"=>"desc","width"=>"5%");
	$col[1] = array("title"=>"ชื่อ - นามสกุล","name"=>"","hid"=>"desc","width"=>"15%");
	$col[2] = array("title"=>"ผู้จัดการ","name"=>"","hid"=>"desc","width"=>"15%");
	$col[3] = array("title"=>"หัวหน้าหน่วย","name"=>"","hid"=>"desc","width"=>"15%");
	$col[4] = array("title"=>"ประจำจังหวัด","name"=>"","hid"=>"desc","width"=>"15%");		
	$col[5] = array("title"=>"รับผิดชอบชุมสาย","name"=>"","hid"=>"desc","width"=>"15%");		
	
	$column .="<tr>\n";

    foreach ($col as $key=>$value) {
     $ahefSort =$col[$key]['title'];
     $column.="	<td align=\"center\" height=\"25px\" width=\"".$col[$key]['width']."\" class=\"formcolumn\" title=\"".$col[$key]['hid']."\">".$ahefSort."</td>\n";
    }
    $column.="</tr>\n";

    $sql = "SELECT eo.emp_name, eo.mobile,mainternace.location_id, em.emp_name as em, es.emp_name as es, pv.province_desc,";
	$sql .="po.position_desc, eo.emp_id as eo, eo.flag_req_spacial FROM employee AS eo Inner Join mainternace ON eo.emp_id = mainternace.e_id ";
	$sql .="Inner Join employee AS em ON mainternace.m_id = em.emp_id Inner Join employee AS es ON mainternace.s_id = es.emp_id ";
	$sql .="Inner Join province AS pv ON pv.province_id = eo.province_id Inner Join `position` AS po ON po.pos_id = eo.pos_id ";
	$sql .="WHERE $sql_pos =  '$empid' ";
	$rs = $db->Execute($sql);
	$num = 1;
	while($arr = $rs->FetchRow()) {
		$emp_name = $arr['emp_name'];									    
	    $em = $arr['em'];
	    $es = $arr['es'];
	    $eo = $arr['eo'];
	    $location_id = $arr['location_id'];		
	    $province_desc = $arr['province_desc'];	    		
	    
	    if($num%2) $c="odd"; else $c="even";
	    $result_data .="<tr id='row_".$offset."' class=\"".$c."\" onClick=\"checkClassTR(this,'".$c."');\">\n";
	    $result_data .="	<td align=\"center\" >" . $num. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $emp_name. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $em. "</td>\n";		    		    	    	    	    
	    $result_data .="	<td align=\"center\" >" . $es. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $province_desc. "</td>\n";
	    $result_data .="	<td align=\"center\" >" . $location_id. "</td>\n";	    
	    $result_data .="</tr>\n";	  
	    $num++;  
	}
	
	$rs->close();
	
	$sql = "select emp_name, flag_req_spacial from employee where emp_id=?";
	$stmt = $db->Prepare($sql);
	$rs = $db->Execute($stmt, array($empid));
	if($arr = $rs->FetchRow()){
		$empname = $arr['emp_name'];		
		$flag = $arr['flag_req_spacial'];		
	}
	$rs->close();
	$head = '<fieldset><legend class="txt_module_head">'.$empname.' รับผิดชอบชุมสาย</legend>';	
	if($pos_id == 3)
	$imgEdit = "<br /><input type='image' src='images/add.gif' border=0 title='แก้ไขผู้รับผิดชอบชุมสาย' onclick=\"cmdExch('$empid','$emp_name','$flag')\"><span class=\"txt_module_head\">&nbsp;&nbsp;แก้ไขผู้รับผิดชอบชุมสาย</span><div id=\"editexch\"></div>\n";
    $result_data = '<table width="100%" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCCC">'.$column.''.$result_data.'</table>'.$imgEdit;
    return $head.$result_data;
        
	
}

function reqALarm($db){
	
	$sql = "SELECT e.location_id, e.exch_name, e.exch_addr, e.province_id FROM exchange AS e WHERE e.province_id = ? order by location_id";
	$stmt = $db->Prepare($sql);
	$arr_pre = array($province_id);
	$rs = $db->Execute($stmt, $arr_pre);
	$html = '';
	$chk = true;
	$html .='<table width="100%" border="0" cellspacing="3" cellpadding="2">';
	
	while($arr1 = $rs->FetchRow()){
		$html .='<tr>';
		if($arr1){
			$html .='<td width="25%" align="rigth" class="line_dash" ><input type="checkbox" name="location" value="'.$arr1['location_id'].'"/><span class="txt_module">'.$arr1['location_id'].'</span></td>';
		}else{
			$html .='<td width="25%" align="rigth" class="line_dash" >&nbsp;</td>';
		}
		if($arr2 = $rs->FetchRow()){
			$html .='<td width="25%" align="rigth" class="line_dash" ><input type="checkbox" name="location" value="'.$arr2['location_id'].'"/><span class="txt_module">'.$arr2['location_id'].'</span></td>';
		}else{
			$html .='<td width="25%" align="rigth" class="line_dash" >&nbsp;</td>';
		}
		if($arr3 = $rs->FetchRow()){
			$html .='<td width="25%" align="rigth" class="line_dash" ><input type="checkbox" name="location" value="'.$arr3['location_id'].'"/><span class="txt_module">'.$arr3['location_id'].'</span></td>';
		}else{
			$html .='<td width="25%" align="rigth" class="line_dash" >&nbsp;</td>';
		}
		$html .='</tr>';	
	}
	$rs->close();
	
	$html .='</tr></table><div id="showExch"></div>';			  
	
}
function writelog($filename, $data) {
    $data = "\n[".date("Y-m-d H:i:s")."]  INFO ". $data;
    $handle = fopen($filename, 'a+');
    fwrite($handle, $data);
    fclose($handle);
}

function getAlarmEmp($db, $empid){
	$sql = "SELECT r.alarm_num FROM req_alarm_spacial AS r WHERE r.emp_id = ?";
	$stmt = $db->Prepare($sql);
	$rs = $db->Execute($stmt, array($empid));
	$index = 0;
	while($arr = $rs->FetchRow()){
		$arrAlarm[$index] = $arr['alarm_num'];
		$index++;
	}
	return $arrAlarm;
}

function getALarmDesc($db, $empid){
	
	$arrAlarm = getAlarmEmp($db, $empid);
	print_r($arrAlarm);
	$sql = "SELECT a.alarm_num, a.alarm_desc_en FROM alarm_desc AS a ORDER BY a.alarm_num ASC ";
	$rs = $db->Execute($sql);
	$ret = "";
	while($arr = $rs->FetchRow()){
		$alarm_num = $arr['alarm_num'];
		if(array_search($alarm_num, $arrAlarm) > -1){
			$ret .= '<option value="'.$alarm_num.'"  class="required">'.$alarm_num.'_'.$arr['alarm_desc_en'].'</option>';
		}else{
			$ret .= '<option value="'.$alarm_num.'" >'.$alarm_num.'_'.$arr['alarm_desc_en'].'</option>';
		}
		
	}
	return $ret;
	
}
function getProvince($db, $prov = ""){
	$sql = "select * from province order by province_desc";	
	$rs = $db->Execute($sql);
	$ret = "";
	while($arr = $rs->FetchRow()){
		if($arr['province_id'] == $prov){
			$ret .= '<option value="'.$arr['province_id'].'" selected>'.$arr['province_desc'].'</option>';
		}else{
			$ret .= '<option value="'.$arr['province_id'].'">'.$arr['province_desc'].'</option>';
		}
		
	}
	return $ret;
}

function getEmployee($db, $pos, $empid){
	$sql = "select * from employee where pos_id='$pos' order by emp_name";	
	$rs = $db->Execute($sql);
	$ret = "";
	while($arr = $rs->FetchRow()){
		if($arr['emp_id'] == $empid){
			$ret .= '<option value="'.$arr['emp_id'].'" selected >'.$arr['emp_name'].'</option>';
		}else{
			$ret .= '<option value="'.$arr['emp_id'].'">'.$arr['emp_name'].'</option>';
		}
		
	}
	return $ret;
}

?>